﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Phone
{
    //自定义信号编辑界面
    public partial class SignalInfo : Form
    {
        private int n = -1;

        //默认构造函数
        public SignalInfo()
        {
            InitializeComponent();
        }

        //带参数的构造函数,参数为要编辑的自定义信号序号
        public SignalInfo(int n)
        {
            InitializeComponent();
            this.n = n;
            textBox1.Text = Vars.custom_signal[n];
            button2.Text = "保存";
        }

        //窗体加载事件
        private void SignalInfo_Load(object sender, EventArgs e)
        {

        }

        //窗体激活时聚焦到输入框
        private void SignalInfo_Activated(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        //窗体关闭时退出程序
        private void SignalInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Form.ActiveForm == this) Application.Exit();
        }

        //返回自定义信号设置界面
        private void button1_Click(object sender, EventArgs e)
        {
            Signals signals = new Signals();
            signals.Show();
            signals.Location = this.Location;
            this.Close();
        }

        //保存或添加自定义信号
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入快捷信号");
                return;
            }
            if (n == -1 && Vars.custom_signal_count >= Defines.MAX_CUSTOM_SIGNAL_COUNT)
            {
                MessageBox.Show("自定义信号数量已达到上限");
                return;
            }
            for (int i = 0; i < Vars.custom_signal_count; i++)
            {
                if (i == n) continue;
                if (Vars.custom_signal[i] == textBox1.Text)
                {
                    MessageBox.Show("该快捷信号已存在");
                    return;
                }
            }
            if (n == -1)
            {
                Vars.custom_signal[Vars.custom_signal_count] = textBox1.Text;
                Vars.custom_signal_count++;
                MessageBox.Show("添加成功");
            }
            else
            {
                Vars.custom_signal[n] = textBox1.Text;
                MessageBox.Show("保存成功");
            }
            button1_Click(sender, e);
        }
    }
}
